﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Threading;

namespace DungeonAndDragonApplication.Controller.Network.Client
{
    public class ClientManager
    {
        private Dictionary<int, Client> clientList;
        private static ClientManager cm;

        public static ClientManager getInstance()
        {
            if (cm == null)
            {
                cm = new ClientManager();
                return cm;
            }
            else
                return cm;
        }

        private ClientManager()
        {
            clientList = new Dictionary<int, Client>();
        }

        
        public void addClient(string ipAddress, int port, ControllerManager cm)
        {
            Client c = new Client(cm);

            c.ConnectToServer(ipAddress, port);
            int index = clientList.Count + 1;
            clientList.Add(index, c);
        }

        public void SendMessageToClient(int clientId, string message)
        {
            if (clientList.Count == 0)
                return;

            try
            {
                clientList[clientId].StringInput = message;
            }
            catch (StackOverflowException exception)
            {
                Console.WriteLine(exception.Message);
            }
        }
    }
}
